<?php

namespace app\admin\controller;

use app\service\FileService;

class FileController
{
    /** @var FileService */
    protected $service;

    function __construct()
    {
        $this->service = app(FileService::class);
    }

    function upload()
    {
        $file = request()->file('file');
        if (empty($file)) {
            return fail(99, 'Invalid File');
        }
        $error = $file->getError();
        if ($error) {
            return fail($error, $file->getErrorMessage());
        }
        try {
            return success($this->service->saveFile($file));
        } catch (\Exception $e) {
            return fail(99, $e->getMessage());
        }
    }
}
